#ifndef DOUBAN_DOUBANCONTEXT_P_H
#define DOUBAN_DOUBANCONTEXT_P_H

#include "douban_global.h"
#include "data/Contacts.h"
#include "internal/DoubanFrame.h"
#include "utils/OAuthClient.h"
#include "utils/ui/PixmapTextObject.h"
#include <QNetworkAccessManager>
#include <QPropertyAnimation>
#include <QButtonGroup>
#include <QPushButton>
#include <QByteArray>
#include <QLineEdit>
#include <QString>
#include <QLabel>
#include <QMovie>
class QNetworkReply;
namespace TWCore
{
	class GlobalInputter;
}

namespace Douban
{
	class FMModule;
	class MailModule;
	class DoubanContext;
	class MiniBlogModule;
	class NotificationModule;

	class DoubanContextPrivate : QObject
	{
		Q_OBJECT
		private: // UIs
			QFrame		m_frame;
			QWidget*	m_miniblogWidget;
			QWidget		m_mailWidget;
			// toolbar
			QFrame		m_toolBarFrame;
			QLabel		m_mainPageBtn;
			QPushButton m_homeBtn;
			QPushButton m_fmBtn;
			QPushButton m_sayingBtn;
			QPushButton m_mailBtn;
			QLabel		m_unreadMailCountLabel;
			QPushButton m_searchBtn;
			// search
			QFrame		m_searchFrame;
			QPushButton m_goSearchBtn;
			QLineEdit	m_searchLine;
			// errors
			QWidget		m_errorWidget;
			QLabel		m_errorLabel;
			QPushButton	m_errorBtn;

			QMovie				m_loadingMovie;
			QButtonGroup		m_btnGroup;
			QPropertyAnimation	m_animation;
			QPropertyAnimation	m_animation2;

			enum FrameState{ DHome = 1, DMail };
			int		e_state;
			uint	i_tweetTimeStamp;
			bool	b_inited;
			bool	b_supportExtraFunction;
			QString s_email;
			QString s_password;
			QString s_ckCookieValue;

			DoubanContext*			m_pImpl;
			QNetworkAccessManager	m_manager;
			QNetworkAccessManager*	m_extraFunctionManager;
			Contacts				m_contacts;
			TWUtils::OAuthClient	m_client;
			TWCore::GlobalInputter*	m_globalInputter;
			DoubanUser				m_messageTarget;
			QList<QNetworkCookie>	fmCookies;
			// Modules
			MiniBlogModule*			m_miniblogModule;
			MailModule*				m_mailModule;
			FMModule*				m_fmModule;
			NotificationModule*		m_notifyModule;
			// Replies
			QNetworkReply*			r_testAccountReply;
			QNetworkReply*			r_postSayingReply;
			QNetworkReply*			r_leaveMessageReply;

			void postSaying(const QString&);
			void postMessage(const QString&);

		public slots:
			void getSelfInfo();
		private slots:
			void updateUserIcon();
			void setFMModuleEnabled(bool);

			void invokeInputer();
			void toogleSearchFrame();
			void goSearch();

			void changeState(int);
			void animationFinished();

			void networkFinished(QNetworkReply*);
			void extraNetworkFinished(QNetworkReply*);
			void getSelfInfoFinished(bool);
			void accountTestFinished();
			void postSayingFinished();
			void postMessageFinished();

		public:
			bool eventFilter(QObject *, QEvent *);
			~DoubanContextPrivate();
		private:
			bool setup();
			void setupFrameUi();
			void configChanged();
			void disableExtraFunction();
			DoubanContextPrivate(const QByteArray&,DoubanContext*);

			static const QString Login_URL;
			static const QString Request_Body;
			static const QString Authorize_URL;
			static const QString RequestToken_URL;
			static const QString AccessToken_URL;
			static const QString Post_Entry;
			static const QString Saying_URL;
			static const QString Message_Entry;
		friend class DoubanContext;
	};
}

#endif /* DOUBANCONTEXT_P_H_ */
